import java.util.Deque;
import java.util.LinkedList;

public class Solution {
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    /*该题的关键点是不要遗漏情况：共四种情况*/
    public boolean isValid (String s) {
        int len = s.length();
        if (len % 2 == 1) return false;
        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < len; i++) {
            char temp = s.charAt(i);
            if (temp == '(') stack.push(')');
            else if (temp == '{') stack.push('}');
            else if (temp == '[') stack.push(']');
            else if (stack.isEmpty() || temp != stack.pop()) return false;
            else stack.pop();
        }
        return stack.isEmpty();
    }
}
